<?php

namespace App\Http\Resources\panyuan;

use App\model\Workshop;
use Illuminate\Http\Resources\Json\ResourceCollection;

class UserWorkshopCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'code'=>'success',
            'data'=> $this->collection->map(function($item){

                    if($item->workshop->type==0){
                        $price=$item->workshop->rent!=0 ? $item->workshop->rent.Workshop::RENT  : '面议';
                    }else{
                        $price=$item->workshop->price!=0 ? $item->workshop->rent.Workshop::SALE  : '面议';
                    }
                    return [
                        'workshop_id'=>$item->workshop->id,
                        'logo'=>$item->workshop->logo.Workshop::LOGO,
                        'title'=>$item->workshop->title,
                        'area'=>$item->workshop->area,
                        'price'=>$price,
                        'tags'=>$item->workshop->tags->reject(function($item,$key){
                            return $key>2;
                        }),
                        'qu'=>$item->workshop->qu->title ?? '',
                        'shenhe'=>$item->workshop->shenhe,
                        'times'=>substr($item->updated_at,0,10),
                    ];

            })
        ];
    }
}
